package com.um.psystem.service.mtService.impl;

import com.um.psystem.mapper.platform.PublicMapper;
import com.um.psystem.service.mtService.IAssetsBalanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: AiLen2E
 * @Date: 2021/11/29 10:53
 * @version: 1.0
 */
@Service
public class AssetsBalanceService implements IAssetsBalanceService {

    @Autowired
    PublicMapper publicMapper;

    @Override
    public List<Map<String, Object>> getAssetsBalance(Map map) {
        String sqltmp = " WHERE 1=1";

        Map paramsMap = new HashMap();
        Integer page = Integer.parseInt((map.get("page") != null ? map.get("page").toString() : "0"));
        Integer rows = Integer.parseInt((map.get("rows") != null ? map.get("rows").toString() : "0"));

        if (map.containsKey("type_dtl_id") && (!map.get("type_dtl_id").toString().equals(""))) {
            String type_dtl_id = map.get("type_dtl_id").toString();
            paramsMap.put("type_dtl_id", type_dtl_id);
            sqltmp +=" AND dtl.type_dtl_id=#{type_dtl_id}";
        }

        if (map.containsKey("type_dtl_no") && (!map.get("type_dtl_no").toString().equals(""))) {
            String type_dtl_no = map.get("type_dtl_no").toString();
            paramsMap.put("type_dtl_no", type_dtl_no);
            sqltmp +=" AND dtl.type_dtl_no=#{type_dtl_no}";
        }

        if (map.containsKey("type_dtl_name") && (!map.get("type_dtl_name").toString().equals(""))) {
            String type_dtl_name = map.get("type_dtl_name").toString();
            paramsMap.put("type_dtl_name", type_dtl_name);
            sqltmp += " AND dtl.type_dtl_name LIKE CONCAT('%',#{type_dtl_name},'%')";
        }

        String sql = "SELECT COUNT(*) AS totals FROM " +
                "(SELECT type_dtl_id,SUM(num) AS receipt FROM ws_eng_assets_stockin_record a GROUP BY a.type_dtl_id) tb_receipt " +
                "LEFT JOIN " +
                "(SELECT b.assets_id,SUM(b.delivery_num) AS delivery FROM ws_eng_assets_delivery_record b GROUP BY b.assets_id) tb_delivery " +
                "ON type_dtl_id=assets_id " +
                "RIGHT JOIN ws_eng_assets_type_dtl dtl ON dtl.type_dtl_id=tb_receipt.type_dtl_id"+sqltmp;

        paramsMap.put("sqlStr", sql);
        int totals = 0;
        List<Map<String, Object>> list_count = publicMapper.getPublicItems(paramsMap);
        if (list_count.size() > 0) {
            totals = Integer.parseInt(list_count.get(0).get("totals").toString());
        } else {
            System.out.println("****************:" + totals);
        }

        sql = "SELECT dtl.type_dtl_id,dtl.type_dtl_name,dtl.type_dtl_no,dtl.k3_code,dtl.sizes,receipt,delivery,receipt-IFNULL(delivery,0) AS balance,dtl.unit,"+totals+" AS totals FROM " +
                "(SELECT type_dtl_id,SUM(num) AS receipt FROM ws_eng_assets_stockin_record a GROUP BY a.type_dtl_id) tb_receipt " +
                "LEFT JOIN " +
                "(SELECT b.assets_id,SUM(b.delivery_num) AS delivery FROM ws_eng_assets_delivery_record b GROUP BY b.assets_id) tb_delivery " +
                "ON type_dtl_id=assets_id " +
                "RIGHT JOIN ws_eng_assets_type_dtl dtl ON dtl.type_dtl_id=tb_receipt.type_dtl_id"+sqltmp+" ORDER BY receipt DESC";

        if (page != 0 && rows != 0) {
            sql += " limit #{startIndex},#{rows} ";
            paramsMap.put("startIndex", (page - 1) * rows);
            paramsMap.put("rows", rows);
        }
        paramsMap.put("sqlStr", sql);
        return publicMapper.getPublicItems(paramsMap);
    }
}
